package com.freesky.biz.activity.controller;

import com.alibaba.fastjson2.JSON;
import com.freesky.biz.activity.domain.TAlipayCouponDetail;
import com.freesky.biz.activity.domain.TCouponDetail;
import com.freesky.biz.activity.domain.TStockMonitor;
import com.freesky.biz.activity.service.ICouponDetailService;
import com.freesky.biz.activity.service.IStockMonitorService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.annotation.Log;
import com.freesky.common.annotation.RepeatSubmit;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/activity/coupon/detail")
public class CouponDetailController extends BaseController {
    @Autowired
    private ICouponDetailService couponDetailService;

    /**
     * 获取券明细列表(微信立减金）
     */
    @PreAuthorize("@ss.hasPermi('activity:coupondetail:list')")
    @GetMapping("/list")
    public TableDataInfo list(TCouponDetail couponDetail) {
        startPage();
        List<TCouponDetail> list = couponDetailService.findList(couponDetail);
        return getDataTable(list);
    }

    /**
     * 获取券明细列表（支付宝红包）
     */
    @PreAuthorize("@ss.hasPermi('activity:coupondetail:list')")
    @GetMapping("/listAlipay")
    public TableDataInfo listAlipay(TCouponDetail couponDetail) {
        startPage();
        List<TCouponDetail> list = couponDetailService.findAlipayList(couponDetail);
        return getDataTable(list);
    }

    @Log(title = "补发券", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('activity:coupondetail:grant')")
    @PostMapping("/grantCoupon")
    public AjaxResult grantCoupon(@RequestBody TCouponDetail couponDetail,
                             HttpServletResponse response) {
        if(StringUtils.isEmpty(couponDetail.getStockId())){
            return AjaxResult.error("券号不能为空!");
        }
        couponDetail.setId(SeqUtil.getSeqId());
        couponDetail.setReissueFlag("1");
        couponDetail.setRemarks("补发券("+getUsername()+")");
        return couponDetailService.grantCoupon(couponDetail);
    }


    @Log(title = "补发支付宝券", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PreAuthorize("@ss.hasPermi('activity:coupondetail:grant')")
    @PostMapping("/grantAlipayCoupon")
    public AjaxResult grantAlipayCoupon(@RequestBody TAlipayCouponDetail couponDetail,
                                  HttpServletResponse response) {
        if(StringUtils.isEmpty(couponDetail.getBusiId())
                || StringUtils.isEmpty(couponDetail.getStockId())){
            return AjaxResult.error("券号或业务ID不能为空!");
        }

        couponDetail.setId(SeqUtil.getSeqId());
        couponDetail.setReissueFlag("1");
        return couponDetailService.grantAlipayCoupon(couponDetail);
    }
    /**
     * 获取券明细列表
     */
    @PreAuthorize("@ss.hasPermi('activity:coupondetail:listByBusiId')")
    @GetMapping("/listByBusiId")
    public AjaxResult listByBusiId(String busiId) {
        List<TCouponDetail> list = couponDetailService.selectListByBusiId(busiId);
        return AjaxResult.success("操作成功!",list);
    }

    @Log(title = "发券明细", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('activity:coupondetail:export')")
    @PostMapping("/export")
    public AjaxResult export(TCouponDetail couponDetail,
                             HttpServletResponse response) {
        return couponDetailService.exportFile(couponDetail);
    }

}
